package com.loongxun.playwithyou.ui.activity.user;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.loongxun.playwithyou.ApiInterface;
import com.loongxun.playwithyou.R;
import com.loongxun.playwithyou.bean.SendRequest;
import com.loongxun.playwithyou.network.MyHttpClientRespenseHandler;
import com.loongxun.playwithyou.network.nHttpClient;
import com.loongxun.playwithyou.ui.activity.LoginActivity;
import com.loongxun.playwithyou.util.ToastView;
import com.loongxun.playwithyou.view.MyDialog;

import org.json.JSONObject;

/**
 * 我的设置页面
 * Created by Administrator on 2017/8/18.
 */
public class SetUpActivity extends Activity implements View.OnClickListener{

    //标题
    private TextView tv_top_title,tv_set_user_out;

    private Context context;
    //用户编辑器
    private SharedPreferences share;
    private SharedPreferences.Editor editor;
    private SendRequest send;
    private String user_id;
    private int onClickWho = 0;
    private MyDialog mDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_set_up);
        context = SetUpActivity.this;

        share = context.getSharedPreferences("userInfo", 0);
        editor = share.edit();
        user_id = share.getString("user_id", "");

        findViewById(R.id.iv_back).setOnClickListener(this);
        tv_top_title = (TextView) findViewById(R.id.tv_top_title);
        tv_top_title.setText("我的设置");

        findViewById(R.id.ll_set_pawd).setOnClickListener(this);
        findViewById(R.id.ll_set_change).setOnClickListener(this);
        findViewById(R.id.ll_set_about).setOnClickListener(this);

        tv_set_user_out = (TextView) findViewById(R.id.tv_set_user_out);
        tv_set_user_out.setOnClickListener(this);
        if (user_id.equals("")) {
            tv_set_user_out.setVisibility(View.GONE);
        }else {
            tv_set_user_out.setVisibility(View.VISIBLE);
        }
    }

    private void initData() {
        try {
            send = new SendRequest();
            send.userId = user_id;
            //判断是否绑定手机号
            nHttpClient.getAll(ApiInterface.IS_BINDPHONE, send.toisMemberJson().toString(), isMemberHandler);
        }catch (Exception e){
            ToastView toast = new ToastView(context, "参数错误");
            toast.show();
        }
    }

    @Override
    public void onClick(View view) {
        Intent intent;
        user_id = share.getString("user_id", "");
        switch (view.getId()){
            case R.id.iv_back:
                finish();
                break;
            case R.id.ll_set_pawd:
                if (isLogin(user_id)){
                    onClickWho = 1;
                    initData();
                }
                break;
            case R.id.ll_set_change:
                if (isLogin(user_id)){
                    onClickWho = 2;
                    initData();
                }
                break;
            case R.id.ll_set_about:
                intent = new Intent(context, AboutActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_set_user_out:
                if (isLogin(user_id)){
                    mDialog = new MyDialog(context,"提示","是否退出登录");
                    mDialog.show();
                    mDialog.positive.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mDialog.dismiss();
                            editor.putString("user_phone", "");
                            editor.putString("user_id", "");
                            editor.putString("user_photo","");
                            editor.putString("user_name", "");
                            editor.putString("password", "");
                            editor.commit();//提交修改
                            new ToastView(context,"登录已退出").show();
                            Intent intent = new Intent();
                            intent.putExtra("login", true);
                            setResult(Activity.RESULT_OK, intent);
                            finish();
                        }
                    });
                    mDialog.negative.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mDialog.dismiss();
                        }
                    });
                }
                break;
            default:
                break;
        }
    }

    private boolean isLogin(String user_id) {
        if (user_id.equals("")){
            Intent intent = new Intent(context, LoginActivity.class);
            startActivityForResult(intent,0);
            return false;
        }
        return true;
    }

    private MyHttpClientRespenseHandler isMemberHandler = new MyHttpClientRespenseHandler() {

        @Override
        public void success(JSONObject json) {
            Integer succeed = json.optJSONObject("status").optInt("succeed", 0);
            Intent intent;
            if (onClickWho == 1) {
                if (succeed == 1) {
                    intent = new Intent(context,PasswordChangeActivity.class);
                    startActivity(intent);
                }else {
                    intent = new Intent(context,PhoneChangeActivity.class);
                    startActivity(intent);
                }
            }
            if (onClickWho == 2) {
                if (succeed == 1) {
                    intent = new Intent(context,PhoneChangeActivity.class);
                    startActivity(intent);
                }else {
                    intent = new Intent(context,PhoneChangeActivity.class);
                    startActivity(intent);
                }
            }
        }

        @Override
        public void faile(String json) {
            new ToastView(context, json).show();
        }
    };
}
